package com.wnxy.carl.carlorderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.carl.carlorderassembly.entity.OrderInfo;
import com.wnxy.carl.carlorderassembly.mapper.OrderInfoMapper;
import com.wnxy.carl.carlorderassembly.service.IOrderInfoService;
import com.wnxy.carl.commons.BeanUtilsExt;
import com.wnxy.carl.dto.OrderInfoDTO;
import com.wnxy.carl.dto.PageDTO;
import com.wnxy.carl.param.PageParam;
import com.wnxy.carl.param.orderInfo.GenerateOrderInfoParam;
import com.wnxy.carl.param.orderInfo.GoodsNumberParam;
import com.wnxy.carl.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils.eq;

/**
 * <p>
 * 订单详情表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-11-12
 */
@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements IOrderInfoService {

    @Resource
    private OrderInfoMapper orderInfoMapper;

    //生成订单详情 Boolean
    public void generateOrderInfo(GenerateOrderInfoParam orderInfoParam) {
        log.info("生成订单详情功能：参数= {}",orderInfoParam);
        Integer orderId = orderInfoParam.getOrderId();
        List<GoodsNumberParam> goodsNumberParams = orderInfoParam.getGoodsNumberParams();
        List<OrderInfo> orderInfos = BeanUtilsExt.copyPropertiesList(OrderInfo.class, goodsNumberParams);
        for (int i = 0,lenth=orderInfos.size(); i < lenth; i ++){
           OrderInfo orderInfo = orderInfos.get(i);
           orderInfo.setOrderId(orderId);
           orderInfo.setGoodsTotal(orderInfo.getNumber()*orderInfo.getGoodsPrice());
            int insert = orderInfoMapper.insert(orderInfo);
            if (insert == 0 ){
                int k = 2/0;//报错回滚
            }
        }
    }
    //查看订单详情 PageDTO<OrderInfoDTO>
    public PageDTO<OrderInfoDTO> seeOrderInof(PageParam<Integer> pageParam) {
        log.info("查看订单详情功能：参数= {}",pageParam);
        IPage<OrderInfo> page = new Page<>(pageParam.getPageIndex(), pageParam.getPageSize());
        QueryWrapper<OrderInfo> qw = new QueryWrapper<>();
        qw.eq("order_id", pageParam.getData());
        qw.orderByDesc(true,"order_info_id");
        IPage<OrderInfo> orderInfoIPage = orderInfoMapper.selectPage(page, qw);
        PageDTO<OrderInfoDTO> pageDTO = PageDTO.bulid((Page<OrderInfo>) orderInfoIPage, OrderInfoDTO.class);
        return pageDTO;
    }
}
